package org.code.boot.controller.common;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.code.boot.common.annotation.Log;
import org.code.boot.common.enums.BusinessType;
import org.code.boot.common.utils.poi.ExcelUtil;
import org.code.boot.controller.BaseController;
import org.code.boot.vo.AjaxResult;
import org.code.boot.vo.TableDataInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.code.boot.domain.common.ComComment;
import org.code.boot.service.common.IComCommentService;

/**
 * 通用评论Controller
 * 
 * @author ruoyi
 * @date 2023-08-27
 */
@RestController
@RequestMapping("/common/comment")
public class ComCommentController extends BaseController
{
    @Autowired
    private IComCommentService bizCommentService;

    /**
     * 查询通用评论列表
     */
    @PreAuthorize("@ss.hasPermi('common:comment:list')")
    @GetMapping("/list")
    public TableDataInfo list(ComComment comComment)
    {
        startPage();
        List<ComComment> list = bizCommentService.selectBizCommentList(comComment);
        return getDataTable(list);
    }

    /**
     * 导出通用评论列表
     */
    @PreAuthorize("@ss.hasPermi('common:comment:export')")
    @Log(title = "通用评论", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ComComment comComment)
    {
        List<ComComment> list = bizCommentService.selectBizCommentList(comComment);
        ExcelUtil<ComComment> util = new ExcelUtil<ComComment>(ComComment.class);
        util.exportExcel(response, list, "通用评论数据");
    }

    /**
     * 获取通用评论详细信息
     */
    @PreAuthorize("@ss.hasPermi('common:comment:query')")
    @GetMapping(value = "/{commentId}")
    public AjaxResult getInfo(@PathVariable("commentId") Long commentId)
    {
        return success(bizCommentService.selectBizCommentByCommentId(commentId));
    }

    /**
     * 新增通用评论
     */
    @PreAuthorize("@ss.hasPermi('common:comment:add')")
    @Log(title = "通用评论", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ComComment comComment)
    {
        return toAjax(bizCommentService.insertBizComment(comComment));
    }

    /**
     * 修改通用评论
     */
    @PreAuthorize("@ss.hasPermi('common:comment:edit')")
    @Log(title = "通用评论", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ComComment comComment)
    {
        return toAjax(bizCommentService.updateBizComment(comComment));
    }

    /**
     * 删除通用评论
     */
    @PreAuthorize("@ss.hasPermi('common:comment:remove')")
    @Log(title = "通用评论", businessType = BusinessType.DELETE)
	@DeleteMapping("/{commentIds}")
    public AjaxResult remove(@PathVariable Long[] commentIds)
    {
        return toAjax(bizCommentService.deleteBizCommentByCommentIds(commentIds));
    }

    /**
     * 添加评论
     */
    @PostMapping("/add/{businessType}/{businessId}")
    public AjaxResult addComment(@PathVariable String businessType, 
                                @PathVariable Long businessId,
                                @RequestParam(required = false) Long parentId,
                                @RequestParam String content)
    {
        return bizCommentService.addComment(businessType, businessId, parentId, content);
    }

    /**
     * 获取业务评论树
     */
    @GetMapping("/tree/{businessType}/{businessId}")
    public AjaxResult getCommentTree(@PathVariable String businessType, @PathVariable Long businessId)
    {
        return bizCommentService.getCommentTree(businessType, businessId);
    }
}